package system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.xml.sax.SAXException;

import settings.MasterController;

import dataStorage.DataHandler;
import dataStorage.DataXML;
import entity.Settings;
import exception.IllegalInput;
import exception.NullVariableFound;
import email.EmailManager;
import email.ReceiveMail;

//@author U096819H
public class Init {

	Settings settings;

	// @author U096819H
	public Init() {
	}

	// @author U096819H
	public boolean runInit(MasterController mc)
			throws TransformerConfigurationException, InstantiationException,
			IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException, NullVariableFound, IllegalInput {
		settings = new Settings();
		getObject();
		execRecur(mc);
		mailRead(mc);
		handleGoal(mc);
		updateDate();		
		return true;
	}

	// @author U096819H
	private void handleGoal(MasterController mc)
			throws TransformerConfigurationException, InstantiationException,
			IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException {
		Calendar today = Calendar.getInstance();
		Calendar startDate = (Calendar) settings.getCal().clone();
		long daysBetween = 0;
		today.add(Calendar.DAY_OF_MONTH, -1);
		mc.AdjustToSaveAmount(settings.getToBeSaveAmt());
		mc.InitializeSavingPot(settings.getSavingPotAmt());
		while (startDate.before(today)) {
			startDate.add(Calendar.DAY_OF_MONTH, 1);
			mc.HandleGoal(startDate);
			daysBetween++;
		}
		settings.setSavingPotAmt(mc.GetSavingPotAmount());
	}

	// @author U096819H
	// save date of the latest session
	private void updateDate() throws TransformerConfigurationException,
			JAXBException, ParserConfigurationException, SAXException,
			IOException, TransformerException, InstantiationException,
			IllegalAccessException {
		Calendar cal = Calendar.getInstance();
		settings.setCal(cal);
		DataHandler dataHandler = DataXML.getInstance();
		dataHandler.update("today", settings, "settings", Settings.class);
	}

	// @author U096819H
	private void getObject() throws TransformerConfigurationException,
			InstantiationException, IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException {
		DataHandler dataHandler = DataXML.getInstance();
		ArrayList<Settings> list = dataHandler.load("settings", Settings.class);
		settings = (Settings) list.get(0);
	}

	// @author U096819H
	// execute recurring expense
	private void execRecur(MasterController mc)
			throws TransformerConfigurationException, InstantiationException,
			IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException, NullVariableFound, IllegalInput {
		// run loop from date to today
		Calendar today = Calendar.getInstance();
		Calendar startDate = (Calendar) settings.getCal().clone();
		System.out.println(today.getTime());
		System.out.println(startDate.getTime());
		long daysBetween = 0;
		today.add(Calendar.DAY_OF_MONTH, -1);
		while (startDate.before(today)) {
			startDate.add(Calendar.DAY_OF_MONTH, 1);
			System.out.println("::" + startDate.getTime());
			mc.HandleRecursIncome(startDate);
			mc.HandleRecursExpense(startDate);
			daysBetween++;
		}
		System.out.println("::::  " + daysBetween);
	}

	public void updateSavingPot(double _updatedValue)
			throws TransformerConfigurationException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException, InstantiationException,
			IllegalAccessException {
		settings.setSavingPotAmt(_updatedValue);
		DataHandler dataHandler = DataXML.getInstance();
		dataHandler.update("today", settings, "settings", Settings.class);
	}

	public void updateToSaveAmt(double _newToSaveAmt)
			throws TransformerConfigurationException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException, InstantiationException,
			IllegalAccessException {
		settings.setToBeSaveAmt(_newToSaveAmt);
		DataHandler dataHandler = DataXML.getInstance();
		dataHandler.update("today", settings, "settings", Settings.class);
	}

	public Settings getSetting() {
		return settings;
	}

	// @author U096819H
	// execute auto mail read
	public void mailRead(MasterController mc)
			throws TransformerConfigurationException, InstantiationException,
			IllegalAccessException, JAXBException,
			ParserConfigurationException, SAXException, IOException,
			TransformerException {
		EmailManager em = new EmailManager();
		em.receive(mc);
	}

}
